package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

public class TaskCompletedMsgHandler implements MsgHandler {
    @Autowired
    private VendingMachineService machineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //将接受到的JSON转换为TaskCompleteContract对象，这里使用的是JSON工具
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        //根据售货机编号查询，再将这个售货机的状态更新
        String innerCode = taskCompleteContract.getInnerCode();
        LambdaQueryWrapper<VendingMachineEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VendingMachineEntity::getInnerCode,innerCode);
        VendingMachineEntity machineEntity = machineService.getOne(wrapper);

        //如果工单类型为补货工单，则将售货机的状态改为运营
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_SUPPLY) {
            machineEntity.setVmStatus(VMSystem.VM_STATUS_RUNNING);
        }
        //如果工单类型未撤机，则将售货机的状态改为撤机
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            machineEntity.setVmStatus(VMSystem.VM_STATUS_REVOKE);
        }
        machineService.updateById(machineEntity);
    }
}
